<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/user/' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/user/staff/' }"
        >员工列表
      </el-breadcrumb-item>
      <el-breadcrumb-item>新增员工</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form
          :model="form"
          :rules="rules"
          label-width="80px"
          ref="form"
          style=" width: 400px;"
        >
          <el-form-item label="账号" prop="accountName">
            <el-input v-model="form.accountName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passwd">
            <el-input
              autocomplete="off"
              show-password
              type="password"
              v-model="form.passwd"
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="repasswd">
            <el-input
              autocomplete="off"
              show-password
              type="password"
              v-model="form.repasswd"
            />
          </el-form-item>
          <el-form-item label="姓名" prop="realName">
            <el-input v-model="form.realName"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item label="工作区域" prop="address">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <el-form-item label="员工类别" prop="authGroupId">
            <el-select placeholder="请选择员工类别" v-model="form.authGroupId">
              <el-option
                :key="item.id"
                :label="item.groupName"
                :value="item.id"
                v-for="item in authGroupList"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="隶属公司" prop="companyId">
            <el-select placeholder="请选择隶属公司" v-model="form.companyId">
              <el-option
                :key="item.id"
                :label="item.companyName"
                :value="item.id"
                v-for="item in companyList"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="简介" prop="remark">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即创建
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    const validateRePasswd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.passwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      form: {
        accountName: "",
        realName: "",
        passwd: null,
        repasswd: null,
        companyId: null,
        authGroupId: null,
        mobile: "",
        address: "",
        remark: ""
      },
      rules: {
        accountName: [
          { required: true, message: "请输入账户", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        realName: [
          { required: true, message: "请输入姓名", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        passwd: [
          { required: true, message: "请输入密码", trigger: "none" },
          { min: 3, max: 25, message: "长度在 3 到 25 个字符", trigger: "none" }
        ],
        repasswd: [
          { required: true, message: "请再次输入密码", trigger: "none" },
          { validator: validateRePasswd, trigger: "none" }
        ],
        companyId: [
          { required: true, message: "请选择所属企业", trigger: "none" }
        ],
        authGroupId: [
          { required: true, message: "请选择员工类别", trigger: "none" }
        ]
      },
      companyList: [],
      authGroupList: []
    };
  },
  mounted() {
    this.companyOptions();
    this.authGroupOptions();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.staff
            .add(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "新增员工成功!"
                });
                this.$router.push("/user/staff/");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/user/staff/");
    },
    companyOptions() {
      this.$ajax.company
        .options()
        .then(result => {
          if (result.code == 1) {
            this.companyList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    authGroupOptions() {
      this.$ajax.auth_group
        .options()
        .then(result => {
          if (result.code == 1) {
            this.authGroupList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    }
  }
};
</script>
